﻿using System;
using MammothMicro.SilverSocket.Protocol.System;
using SilverSocket.Shared;

namespace MammothMicro.SilverSocket.Shared
{
    /// <summary>
    /// Class processes incoming messages and raises appropriate events
    /// </summary>
    public abstract class MessageHandlerBase
    {
        private HandledMessageDictionary _handledMessageDictionary;

        public MessageHandlerBase(HandledMessageDictionary handledMessageDictionary)
        {
            this._handledMessageDictionary = handledMessageDictionary;
        }

        /// <summary>
        /// Process incoming messages from clients and raise proper events
        /// </summary>
        /// <param name="sessionId">
        /// </param>
        /// <param name="message">
        /// </param>
        public abstract void ProcessMessage(string sessionId, IDuplexCbMessage message);

        /// <summary>
        /// Register all message types handled by this handler
        /// </summary>
        /// <param name="handledTypes">
        /// The handled Types.
        /// </param>
        protected void RegisterHandledMessages(Type[] handledTypes)
        {
            if (handledTypes == null)
                return;

            foreach (var type in handledTypes)
            {
                if (!this._handledMessageDictionary.ContainsKey(type))
                    this._handledMessageDictionary.Add(type, this.GetType());
            }
        }

        public abstract void RegisterHandledMessages();
    }
}